package com.nvn.washiodriver.activities;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.nvn.washiodriver.R;
import com.nvn.washiodriver.Objects.Order;
import com.nvn.washiodriver.network.WebServiceCommunicatorListener;
import com.nvn.washiodriver.network.ws.BaseWSControl;
import com.nvn.washiodriver.network.ws.BaseWSControl.WebServiceFlag;
import com.nvn.washiodriver.network.ws.UpdateNoteWSControl;
import com.nvn.washiodriver.utils.SessionManager;
import com.nvn.washiodriver.utils.Statics;

public class NoteActivity extends Activity implements OnClickListener,
		WebServiceCommunicatorListener {

	private Order mOrder;
	private EditText mEdtNote;
	private TextView mTvCancel, mTvSave, mTvTitle;
	private ProgressDialog pd;
	private String curNote;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		overridePendingTransition(R.anim.slide_in_from_right, R.anim.anim_alpha);

		setContentView(R.layout.activity_note);

		initView();
		initProperties();
		initListeners();
	}

	protected void initView() {

		mEdtNote = (EditText) findViewById(R.id.edt_driver_note);
		mTvCancel = (TextView) findViewById(R.id.tv_cancel);
		mTvSave = (TextView) findViewById(R.id.tv_save);
		mTvTitle = (TextView) findViewById(R.id.tv_title_bar);
	}

	private void initProperties() {
		String title = getIntent().getStringExtra("TITLE");
		mTvTitle.setText((title != null) ? title : "Washio");

		mOrder = SessionManager.getSessionManager(this).getCurOrder();
		if (mOrder == null) {
			Statics.showToast(this, "This order is not exist!");
			finish();
		}

		mEdtNote.setText(mOrder.getDriverNote());

		pd = new ProgressDialog(this);
		pd.setCancelable(false);
		pd.setMessage("Loading...");
	}

	private void initListeners() {
		mTvCancel.setOnClickListener(this);
		mTvSave.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tv_cancel:
			finish();
			break;

		case R.id.tv_save:
			curNote = mEdtNote.getText().toString().trim();
			if (curNote.equals(mOrder.getDriverNote())) {
				Statics.showToast(this, "Please enter your notes!");
				return;
			}

			new UpdateNoteWSControl(this, this, mOrder.getObjectId(), curNote)
					.execute();
			break;

		default:
			break;
		}
	}

	@Override
	public void onConnectionOpen(BaseWSControl wsControl, WebServiceFlag flag) {
		pd.setMessage("Loading...");
		pd.show();
	}

	@Override
	public void onConnectionError(BaseWSControl wsControl, WebServiceFlag flag,
			String error) {
		Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
		pd.dismiss();
	}

	@Override
	public void onConnectionDone(BaseWSControl wsControl, WebServiceFlag flag,
			Object result) {
		pd.dismiss();
		if (flag == WebServiceFlag.UPDATE_NOTE) {
			Intent i = getIntent();
			i.putExtra("NOTE", curNote);
			setResult(RESULT_OK, i);
			finish();
		}
	}

	@Override
	public void onUpdateData(BaseWSControl wsControl, WebServiceFlag flag,
			Object data) {
	}
}
